﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP09S03_OfficeTracking
{
    public partial class DiagUser : WP09S03_OfficeTracking.ParentClass.ParentDiag
    {
        private Controller.EmployeController controller;
        private bool bNew;
        
        public DiagUser()
        {
            InitializeComponent();
            bNew = true;
            this.lblSubTitle.Text = "Add User";
            this.controlCurrentPWD.Enabled = false;
            this.controlCurrentPWD.Visible = false;
            this.controlNewPWD.LabelCaption = "Password";
            this.controlConfirmPWD.LabelCaption = "Confirm Password";

        }

        public DiagUser(string id)
        {
            InitializeComponent();
            bNew = false;
            this.lblSubTitle.Text = "Change Password";
            this.controlUserID.UserText = id;
            this.controlUserID.Enabled = false;
         }

        private void buttonSave_Click(object sender, EventArgs e)
        {
           
            if (controlNewPWD.UserText != controlConfirmPWD.UserText)
            {
                SetMessage("New Password can not be confirmed. Please Retry.");
                return;
            }

            try
            {
                if (bNew)
                {
                    controller.AddUser(controlUserID.UserText, 
                    controlNewPWD.UserText);
                }
                else
                {
                    controller.EditUser(controlUserID.UserText, controlCurrentPWD.UserText,
                    controlNewPWD.UserText);
  
                }
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception exp)
            {
                SetMessage(exp.Message);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void DiagUser_Load(object sender, EventArgs e)
        {
            controller = new Controller.EmployeController();
        }
    }
}
